package com.lifo.user.service.serviceimpl;

import com.lifo.user.bean.model.HfUser;
import com.lifo.user.dao.HfUserMapper;
import com.lifo.user.service.HfUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * Created by Administrator on 2018/6/24.
 */
@Slf4j
@Service
public class HfUserServiceImpl implements HfUserService {


    @Autowired
    private HfUserMapper hfUserMapper;


    /**
     * 登陆
     * @param mobile 手机号
     * @return
     */
    @Override
    public HfUser searchHfUserByMobile(String mobile) {
        try{
          return   hfUserMapper.searchHfUserByMobile(mobile);
        }catch (Exception e){
            log.error("通过手机号和密码查询用户失败,异常信息为=====》{}",e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 注册用户
     * @param hfUser
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public Integer saveHfUser(HfUser hfUser) {
        try{
            return hfUserMapper.saveHfUser(hfUser);
        }catch (Exception e){
            log.error("保存错误信息时报错，异常信息为===========》{}",e.getMessage());
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return null;
    }

    /***
     * 判断手机号是否存在
     * @param mobile
     * @return
     */
    @Override
    public Integer searchMobileIsExist(String mobile) {
        try {
            return hfUserMapper.searchMobileIsExist(mobile);
        }catch (Exception e){
            e.printStackTrace();
            log.error("查询手机号是否存在时报错，异常信息为==========》{}",e.getMessage());
        }
        return 0;
    }
}
